//
//  TMMyDiscountViewController.m
//  国学商家管理
//
//  Created by Tao Li on 2018/7/6.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "TMMyDiscountViewController.h"
#import "TMMyDiscountCell.h"
#import "TMUserViewModel.h"
#import "TMNearbyViewModel.h"
#import "TMShopHomeController.h"
#import "TMCommodityDetailController.h"
@interface TMMyDiscountViewController ()
@property (nonatomic, strong) TMUserViewModel *viewModel;
@property (nonatomic, strong) NSMutableArray *listArray;
@property (nonatomic, strong) TMNearbyViewModel *nearModel;
@property (nonatomic, strong) WZNoResultView *noResultView;
@end

@implementation TMMyDiscountViewController
- (void)dealloc{
    NSLog(@"TMMyDiscountViewController dealloc")
}
-(NSMutableArray *)listArray
{
    if (!_listArray) {
        _listArray = [NSMutableArray array];
    }
    return _listArray;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear: animated];
    [self.tableView.mj_header beginRefreshing];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.isMine ?@"我的优惠券":@"领取优惠券";
    self.tableView.tableFooterView = [UIView new];
    //self.tableView.contentInset = UIEdgeInsetsMake(10, 0, 0, 0);
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(begainUse:) name:@"begainToUse" object:nil];
    self.tableView.backgroundColor = [UIColor hx_colorWithHexRGBAString:@"F8F8F8"];
    self.viewModel = [[TMUserViewModel alloc]init];
     self.nearModel = [[TMNearbyViewModel alloc]init];
    __weak typeof(self) weakSelf =  self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (weakSelf.distype == 2) {
            [weakSelf.listArray removeAllObjects];
            [weakSelf.viewModel getMyDiscountSuccess:^(id responseObject) {
                weakSelf.listArray = responseObject;
                [weakSelf noResultViewShowORNot];
                [weakSelf.tableView reloadData];
                [weakSelf.tableView.mj_header endRefreshing];
            } failed:^(NSString *error) {
                
                [weakSelf.tableView.mj_header endRefreshing];
                [weakSelf noResultViewShowORNot];
                [weakSelf.tableView reloadData];
            }];
        }
        if (weakSelf.distype == 1) {
            //ke yong 
            [weakSelf.listArray removeAllObjects];
            NSLog(@"3333333333");
            [weakSelf.nearModel getCanUuseDiscountListWith:weakSelf.cid With:weakSelf.realCid Success:^(id responseObject) {
                weakSelf.listArray = responseObject;
                [weakSelf noResultViewShowORNot];
                [weakSelf.tableView reloadData];
                [weakSelf.tableView.mj_header endRefreshing];
            } failed:^(NSString *error) {
                [weakSelf.tableView.mj_header endRefreshing];
                [weakSelf noResultViewShowORNot];
                [weakSelf.tableView reloadData];
            }];
        }
        if (weakSelf.distype == 0) {
             [weakSelf.listArray removeAllObjects];
//            NSMutableDictionary *dic = [TMUserManager paramDisplay];
//            [dic setValue:@(9) forKey:@"id"];
            //提交订单可以使用的优惠券。
            [weakSelf.nearModel getCourseDiscountWith:weakSelf.cid Success:^(id responseObject) {
                weakSelf.listArray = responseObject;
                [weakSelf noResultViewShowORNot];
                [weakSelf.tableView reloadData];
                [weakSelf.tableView.mj_header endRefreshing];
            } failed:^(NSString *error) {
                [weakSelf noResultViewShowORNot];
                [weakSelf.tableView reloadData];
                [weakSelf.tableView.mj_header endRefreshing];
            }];
        }
        
    }];
    [self.tableView.mj_header beginRefreshing];
}

- (void)begainUse:(NSNotification *)noti{
    [self.listArray removeAllObjects];
    [self.tableView reloadData];
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TMMyDiscountCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TMMyDiscountCell" forIndexPath:indexPath];
    cell.discountType = self.distype;
    cell.model = self.listArray[indexPath.row];
    __weak typeof(self) weakSelf =  self;
    cell.block = ^(NSInteger type, NSInteger soid) {
        if (type == 1) {
            TMShopHomeController *vc = [[UIStoryboard storyboardWithName:@"Nearby" bundle:nil] instantiateViewControllerWithIdentifier:@"TMShopHomeController"];
            vc.shopid = soid;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }
        if (type == 2) {
            TMCommodityDetailController *vc = [[UIStoryboard storyboardWithName:@"Nearby" bundle:nil] instantiateViewControllerWithIdentifier:@"TMCommodityDetailController"];
            vc.rid = soid;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }
    };
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 118+5;
}
- (WZNoResultView *)noResultView{
    if (!_noResultView) {
        _noResultView = [[[NSBundle mainBundle] loadNibNamed:@"WZNoResultView" owner:nil options:nil]firstObject];
        if (kWindowH == 812.0) {
            _noResultView.frame = CGRectMake(0, 0, kWindowW, kWindowH);
        }else {
            _noResultView.frame = CGRectMake(0, 0, kWindowW, kWindowH);
        }
        _noResultView.isSearchVC = NO;
        [self.tableView addSubview:_noResultView];
    }
    return _noResultView;
}
- (void)noResultViewShowORNot{
    if (!self.listArray.count) {
        self.noResultView.hidden = NO;
    }else{
        self.noResultView.hidden = YES;
    }
}
@end
